<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: index.tpl 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
?>
<script>
    var sitepages_likes = function(resource_id, resource_type) {
	  var content_type = 'sitepage';

		// SENDING REQUEST TO AJAX
		var request = createLikepage(resource_id, resource_type,content_type);
		// RESPONCE FROM AJAX
		request.addEvent('complete', function(responseJSON) {
			if (responseJSON.error_mess == 0) {
				if(responseJSON.like_id )
				{  	
					$('sitepage_like_'+ resource_id).value = responseJSON.like_id;
					$('sitepage_most_likes_'+ resource_id).style.display = 'none';
					$('sitepage_unlikes_'+ resource_id).style.display = 'block';	 
				}
				else 
				{  
					$('sitepage_like_'+ resource_id).value = 0;
					$('sitepage_most_likes_'+ resource_id).style.display = 'block';
					$('sitepage_unlikes_'+ resource_id).style.display = 'none';	
				}
			}
			else {
        <?php $var = $this->translate('An error has occurred processing the request. The target may no longer exist.'); ?>
        <?php $var1 = $this->translate('Close'); ?>
				en4.core.showError('<?php echo $var ?>' + '<br /><br /><button onclick="Smoothbox.close()"><?php echo $var1 ?></button>');
				return;
			}
		});   
	}
// FUNCTION FOR CREATING A FEEDBACK 
var createLikepage = function( resource_id, resource_type, content_type )
{ 
	if (content_type == 'sitepage') {
		var like_id = $(content_type + '_like_'+ resource_id).value	
	}   
  var url = '<?php echo $this->url(array('action' => 'global-likes' ), 'sitepage_like', true);?>';
	var request = new Request.JSON({    
		url : url,
		data : {
			format : 'json',
			'resource_id' : resource_id,
			'resource_type' : resource_type,	
			'like_id' : like_id
		}
	});  
	request.send();
	return request;
}		
</script>
<?php 
$viewer = Engine_Api::_()->user()->getViewer()->getIdentity();
  if(!empty($viewer))
  {
    $MODULE_NAME = 'sitepage';
    $RESOURCE_TYPE = 'sitepage_page';
    $RESOURCE_ID = Engine_Api::_()->core()->getSubject()->getIdentity();

    // Check that for this 'resurce type' & 'resource id' user liked or not.
    $check_availability = Engine_Api::_()->$MODULE_NAME()->checkAvailability( $RESOURCE_TYPE, $RESOURCE_ID );
    if( !empty($check_availability) )
    {
      $label = 'Unlike this';
      $unlike_show = "display:block;";
      $like_show = "display:none;";
      $like_id = $check_availability[0]['like_id'];
    }
    else
    {
      $label = 'Like this';
      $unlike_show = "display:none;";
      $like_show = "display:block;";
      $like_id = 0;
    }
  }
  ?>
	<?php if(empty($this->sitepage_like)){ return; } ?>
  <div class="sitepage_like_button" id="sitepage_unlikes_<?php echo $RESOURCE_ID;?>" style ='<?php echo $unlike_show;?>' >
    <a href = "javascript:void(0);" onclick = "sitepages_likes('<?php echo $RESOURCE_ID; ?>', 'sitepage_page');">
      <i class="sitepage_like_thumbdown_icon"></i>
      <span><?php echo $this->translate('Unlike') ?></span>
    </a>
  </div>
  <div class="sitepage_like_button" id="sitepage_most_likes_<?php echo $RESOURCE_ID;?>" style ='<?php echo $like_show;?>'>
    <a href = "javascript:void(0);" onclick = "sitepages_likes('<?php echo $RESOURCE_ID; ?>', 'sitepage_page');">
      <i class="sitepage_like_thumbup_icon"></i>
      <span><?php echo $this->translate('Like') ?></span>
    </a>
  </div>
  <input type ="hidden" id = "sitepage_like_<?php echo $RESOURCE_ID;?>" value = '<?php echo $like_id; ?>' />